<script setup lang="ts">
import { ref, useSlots, watch } from 'vue'

const props = withDefaults(
  defineProps<{
    modelValue?: boolean
  }>(),
  {
    modelValue: false
  }
)

const emit = defineEmits<{
  (e: 'update:modelValue', value: boolean): void
}>()

const slots = useSlots()
// slots.default && slots.default()
const checked = ref(false)

const changeChecked = () => {
  checked.value = !checked.value
  emit('update:modelValue', checked.value)
}
watch(
  () => props.modelValue,
  () => {
    checked.value = props.modelValue
  },
  { immediate: true }
)
</script>

<script lang="ts">
export default {
  name: 'Checkbox'
}
</script>

<template>
  <div class="checkbox" @click="changeChecked()">
    <i v-if="checked" class="iconfont icon-checked"></i>
    <i v-else class="iconfont icon-unchecked"></i>
    <span><slot /></span>
  </div>
</template>

<style lang="less" scoped>
.checkbox {
  display: inline-block;
  margin-right: 2px;
  .icon-checked {
    color: @themeColor;
    ~ span {
      color: @themeColor;
    }
  }
  i {
    position: relative;
    top: 1px;
  }
  span {
    margin-left: 2px;
  }
}
</style>
